package com.wanpinghui.supplier.user.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/12/6
 */

public class BusinessAreaAdapter extends BaseExpandableListAdapter {


    private final List<AreaBean> areaBeanList;
    private Context context;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int groupPosition, int childPosition);

        void onItemGroupClick(int groupPosition);
    }

    public BusinessAreaAdapter(List<AreaBean> areaBeanList, Context context) {
        this.areaBeanList = areaBeanList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return areaBeanList == null ? 0 : areaBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (areaBeanList.get(groupPosition).getChildren() != null) {
            return areaBeanList.get(groupPosition).getChildren().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return areaBeanList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return areaBeanList.get(groupPosition).getChildren();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            groupViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            groupViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);
            groupViewHolder.iv_jiantou = (ImageView) convertView.findViewById(R.id.iv_jiantou);
            groupViewHolder.tv_all = (TextView) convertView.findViewById(R.id.tv_all);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final TextPaint paint = groupViewHolder.textViewContent.getPaint();
        final AreaBean areaBeen = areaBeanList.get(groupPosition);
        if (groupPosition == 0) {
            groupViewHolder.iv_jiantou.setVisibility(View.GONE);
            groupViewHolder.textViewContent.setSelected(true);
            groupViewHolder.imageViewSelect.setVisibility(View.VISIBLE);
            groupViewHolder.tv_all.setVisibility(View.GONE);
            if (isAllSelect(areaBeanList)) {
                groupViewHolder.imageViewSelect.setSelected(true);
            } else {
                groupViewHolder.imageViewSelect.setSelected(false);
            }
        } else {
            groupViewHolder.iv_jiantou.setVisibility(View.VISIBLE);
            if (isExpanded) {
                groupViewHolder.imageViewSelect.setVisibility(View.VISIBLE);
                groupViewHolder.tv_all.setVisibility(View.GONE);
                if (isAllCitySelect(areaBeen.getChildren())) {
                    paint.setFakeBoldText(true);
                    groupViewHolder.textViewContent.setSelected(true);
                    groupViewHolder.imageViewSelect.setSelected(true);
                    areaBeen.setSelect(true);
                    areaBeanList.get(0).setSelect(true);
                } else if (isHaveSelect(areaBeen.getChildren())) {
                    paint.setFakeBoldText(true);
                    groupViewHolder.textViewContent.setSelected(true);
                    groupViewHolder.imageViewSelect.setSelected(false);
                    areaBeen.setSelect(false);
                    areaBeanList.get(0).setSelect(false);
                } else {
                    paint.setFakeBoldText(false);
                    groupViewHolder.textViewContent.setSelected(false);
                    groupViewHolder.imageViewSelect.setSelected(false);
                    areaBeen.setSelect(false);
                    areaBeanList.get(0).setSelect(false);
                }
                groupViewHolder.iv_jiantou.animate().setDuration(500).rotation(180).start();
            } else {
                if (isAllCitySelect(areaBeen.getChildren())) {
                    paint.setFakeBoldText(true);
                    groupViewHolder.textViewContent.setSelected(true);
                    groupViewHolder.tv_all.setVisibility(View.VISIBLE);
                    groupViewHolder.tv_all.setText("服务全省");
                    groupViewHolder.imageViewSelect.setVisibility(View.GONE);
                } else if (isHaveSelect(areaBeen.getChildren())) {
                    paint.setFakeBoldText(true);
                    groupViewHolder.textViewContent.setSelected(true);
                    groupViewHolder.tv_all.setVisibility(View.VISIBLE);
                    groupViewHolder.tv_all.setText("服务部分城市");
                    groupViewHolder.imageViewSelect.setVisibility(View.GONE);
                    areaBeanList.get(0).setSelect(false);
                } else {
                    paint.setFakeBoldText(false);
                    groupViewHolder.textViewContent.setSelected(false);
                    groupViewHolder.imageViewSelect.setVisibility(View.VISIBLE);
                    groupViewHolder.imageViewSelect.setSelected(false);
                    groupViewHolder.textViewContent.setSelected(false);
                    groupViewHolder.tv_all.setVisibility(View.GONE);
                    areaBeanList.get(0).setSelect(false);
                }
                groupViewHolder.iv_jiantou.animate().setDuration(500).rotation(0).start();
            }
        }
        groupViewHolder.imageViewSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groupPosition == 0) {
                    if (isAllSelect(areaBeanList)) {
                        for (int i = 0; i < areaBeanList.size(); i++) {
                            AreaBean bean = areaBeanList.get(i);
                            bean.setSelect(false);
                            if (i != 0) {
                                for (AreaBean bean1 : bean.getChildren()) {
                                    bean1.setSelect(false);
                                }
                            }
                        }
                        areaBeanList.get(0).setSelect(false);
                    } else {
                        for (int i = 0; i < areaBeanList.size(); i++) {
                            AreaBean bean = areaBeanList.get(i);
                            bean.setSelect(true);
                            if (i != 0) {
                                for (AreaBean bean1 : bean.getChildren()) {
                                    bean1.setSelect(true);
                                }
                            }
                        }
                        areaBeanList.get(0).setSelect(true);
                    }
                } else {
                    if (isAllCitySelect(areaBeen.getChildren())) {
                        for (AreaBean bean : areaBeen.getChildren()) {
                            bean.setSelect(false);
                        }
                        areaBeanList.get(0).setSelect(false);
                    } else {
                        for (AreaBean bean : areaBeen.getChildren()) {
                            bean.setSelect(true);
                        }
                    }
                }
                notifyDataSetChanged();
                System.out.println("点击时是否全选"+areaBeanList.get(0).isSelect());

            }
        });
        groupViewHolder.textViewContent.setText(areaBeanList.get(groupPosition).getName());
        return convertView;
    }

    private boolean isAllSelect(List<AreaBean> areaBeanList) {
        if (areaBeanList == null || areaBeanList.size() == 0) return false;
        for (int i = 0; i < areaBeanList.size(); i++) {
            AreaBean bean = areaBeanList.get(i);
            if (i != 0) {
                if (bean.isSelect()) {
                    for (AreaBean bean1 : bean.getChildren()) {
                        if (!bean1.isSelect()) {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public ArrayList<AreaBean> getDatas() {
//        List<BusinessTypeBean> beanList = new ArrayList<>();
//        for (BusinessTypeBean bean: datas){
//            if (bean.isSelect()){
//                beanList.add(bean);
//            }
//        }
        return (ArrayList<AreaBean>) areaBeanList;
    }

    private boolean isHaveSelect(List<AreaBean> areaBeanList) {
        if (areaBeanList == null || areaBeanList.size() == 0)
            return false;
        for (AreaBean bean : areaBeanList) {
            if (bean.isSelect()) {
                return true;
            }
        }
        return false;
    }


    private boolean isAllCitySelect(List<AreaBean> areaBeanList) {
        if (areaBeanList == null || areaBeanList.size() == 0) return false;
        for (AreaBean bean : areaBeanList) {
            if (!bean.isSelect()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final GroupViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            childViewHolder = new GroupViewHolder();
            childViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            childViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            childViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);

            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final AreaBean customerAreachildbean = areaBeanList.get(groupPosition).getChildren().get(childPosition);
        childViewHolder.ll_area.setBackgroundColor(ContextCompat.getColor(context, R.color.colorBackgroundF7));
        childViewHolder.textViewContent.setText(customerAreachildbean.getName());
        if (customerAreachildbean.isSelect()) {
            //  childViewHolder.textViewContent.setSelected(true);
            childViewHolder.imageViewSelect.setSelected(true);
        } else {
            //    childViewHolder.textViewContent.setSelected(false);
            childViewHolder.imageViewSelect.setSelected(false);
        }
        childViewHolder.ll_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customerAreachildbean.isSelect()) {
                    customerAreachildbean.setSelect(false);
                } else {
                    customerAreachildbean.setSelect(true);
                }
                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private class GroupViewHolder {
        TextView textViewContent;
        ImageView imageViewSelect;
        RelativeLayout ll_area;
        ImageView iv_jiantou;
        TextView tv_all;
    }
}
